const net = require("net");
const socket = net.createConnection(
  {
    host: "www.baidu.com",
    port: 80,
  },
  () => {
    console.log("链接成功");
  }
);
/**
 * 提炼出响应字符串的消息头和消息体
 * @param {*}response
 */
function parseResponse(res) {
  const index = res.indexOf("\r\n\r\n");
  console.log(index);
  const head = res.substring(0, index);
  const body = res.substring(index + 2);
  console.log("=======");
  console.log(head);
  console.log(head.split("\r\n"));
  console.log("=======");

  //   console.log(body);
}
let isFirst = true;
socket.on("data", (chunk) => {
  const response = chunk.toString("utf-8");
  console.log("服务器回复测试");
  if (isFirst) {
    parseResponse(response);
    isFirst = false;
  }
  socket.end();
});
socket.write(`GET / HTTP/1.1
Host: www.baidu.com

`);

socket.on("close", () => {
  console.log("挂断电话");
});
